import { Component, OnInit, ContentChildren, QueryList, AfterContentInit, Input } from '@angular/core';
import { I18nItemComponent } from './i18n-item/i18n-item.component';
import { isString } from '../../util';
import { I18nService } from '../../core/i18n/i18n.service';

let groupKeyTick = 0;

@Component({
  selector: 'bz-i18n',
  template: '',
  styles: []
})
export class I18nComponent implements OnInit, AfterContentInit {

  @ContentChildren(I18nItemComponent) items: QueryList<I18nItemComponent>;
  @Input() key = 'auto-group';

  private itemMap = {};

  constructor(private i18nService: I18nService) {
    this.key = `${this.key}-${groupKeyTick++}`;
  }

  ngOnInit() {
  }

  ngAfterContentInit() {
    this.items.forEach((item) => {
      this.itemMap[item.key] = item.value;
    });

    if (isString(this.key) && this.key.trim()) {
      this.i18nService.setGroupMap(this.key, this.itemMap);
    }
  }

  getValue(key: string) {
    return this.itemMap[key];
  }
}
